﻿package 
{
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.events.Event;
	import flash.utils.getDefinitionByName;
	import fl.motion.Color;
	import flash.text.engine.ElementFormat;
	
	public class EquipDetailed extends MovieClip 
	{
		public var Stats:BaseStats;
		public var MainInfo:BasicInfo;
		public var MessageText:String;
		public var ExaminingEquipment:Boolean;
		
		private static var lhandRemoveName:String = "Remove left hand equipment";
		private static var rhandRemoveName:String = "Remove right hand equipment";
		private static var bodyRemoveName:String = "Remove body equipment";
		private static var accessoryRemoveName:String = "Remove accessory equipment";
		
		public function EquipDetailed(stats:BaseStats, basicInfo:BasicInfo) 
		{
			Stats = stats;
			MainInfo = basicInfo;
			
			init();
			loadStats(Stats, Stats, false);
			
			addEventListener(Event.ENTER_FRAME, EnterFrame);
			addEventListener(Event.REMOVED, Unload);
		}
		
		private function init():void
		{
			loadEquipment();
			MessageText = StringHelper.Empty;
			ExaminingEquipment = false;
		}
		
		private function Unload(event:Event):void
		{
			if (event.target.name == this.name)
			{
				removeMenuChildren();
				removeEventListener(Event.ENTER_FRAME, EnterFrame);
			}
		}
		
		private function EnterFrame(event:Event):void
		{
			var selectEquipMenu:MovieClip = null;
			
			if (equipMenuPlaceholder.numChildren > 0)
			{
				selectEquipMenu = (equipMenuPlaceholder.getChildAt(0) as MovieClip);
			}
			if (selectEquipMenu != null && selectEquipMenu.Active)
			{
				var equip:Equipment = selectEquipMenu.SelectedObject as Equipment;
				
				if (equip != null)
				{
					var messageToSet:String = StringHelper.Empty;
					if (!ExaminingEquipment)
					{
						messageToSet += equip.DisplayName;
					}
					else
					{
						if (equip.EquipmentType != Equipment.EquipmentTypeUndefined)
						{
							messageToSet += "Equip ";
						}
						messageToSet += equip.DisplayName + " on " + Stats.Name + "?";
					}
					MessageText = messageToSet;
				}
				
				if (Game.XIsPressed)
				{
					if (ExaminingEquipment)
					{
						loadStats(Stats, Stats, false);
						ExaminingEquipment = false;
					}
				}
				
				if (Game.ZIsPressed)
				{
					var tempStats:BaseStats = Stats.CreateCopy();
					if (!ExaminingEquipment)
					{
						equipEquipment(tempStats, equip);
						loadStats(tempStats, Stats, true);
						ExaminingEquipment = true;
					}
					else
					{
						if (equip.EquipmentType == Equipment.EquipmentTypeUndefined)
						{
							if (equip.DisplayName == lhandRemoveName)
							{
								unequipEquipment(Stats, MainInfo, Equipment.EquipmentTypeLeftHand)
							}
							else if (equip.DisplayName == rhandRemoveName)
							{
								unequipEquipment(Stats, MainInfo, Equipment.EquipmentTypeRightHand)
							}
							else if (equip.DisplayName == bodyRemoveName)
							{
								unequipEquipment(Stats, MainInfo, Equipment.EquipmentTypeArmor)
							}
							else if (equip.DisplayName == accessoryRemoveName)
							{
								unequipEquipment(Stats, MainInfo, Equipment.EquipmentTypeAccessory)
							}
						}
						else
						{
							MainInfo.EquipmentNames.splice((selectEquipMenu.CurrentIndex - 5), 1);
							unequipEquipment(Stats, MainInfo, equip.EquipmentType)
						}
						
						equipEquipment(Stats, equip);
						loadStats(Stats, Stats, false);
						ExaminingEquipment = false;
						loadEquipment();
					}
				}
			}
		}

		private static function unequipEquipment(baseStats:BaseStats, mainInfo:BasicInfo, equipmentType:int):void
		{
			if (equipmentType == Equipment.EquipmentTypeRightHand)
			{
				if (baseStats.RHandEquip.EquipmentType != Equipment.EquipmentTypeUndefined)
				{   
					var clipName:String = flash.utils.getQualifiedClassName(baseStats.RHandEquip);
					mainInfo.EquipmentNames.push(clipName);
				}
			}
		}

		private static function equipEquipment(baseStats:BaseStats, equip:Equipment):void
		{
			if (equip.EquipmentType == Equipment.EquipmentTypeUndefined)
			{
				if (equip.DisplayName == lhandRemoveName)
				{
					baseStats.LHandEquip = new Equipment_None();
				}
				else if (equip.DisplayName == rhandRemoveName)
				{
					baseStats.RHandEquip = new Equipment_None();
				}
				else if (equip.DisplayName == bodyRemoveName)
				{
					baseStats.BodyEquip = new Equipment_None();
				}
				else if (equip.DisplayName == accessoryRemoveName)
				{
					baseStats.AccessoryEquip = new Equipment_None();
				}
			}
			else if (equip.EquipmentType == Equipment.EquipmentTypeRightHand)
			{
				baseStats.RHandEquip = equip;
			}
		}

		private function loadStats(baseStats:BaseStats, originalStats:BaseStats, comparision:Boolean):void
		{
			var statsBasic:StatsBasic = new StatsBasic(Stats);
			statsBasic.width = 185;
			statsBasic.height = 65;
			statsBasicPlaceholder.addChild(statsBasic);
			
			if (!comparision)
			{
				rHandEquip.text = baseStats.RHandEquip.DisplayName;
				lHandEquip.text = baseStats.LHandEquip.DisplayName;
				bodyEquip.text = baseStats.BodyEquip.DisplayName;
				accEquip.text = baseStats.AccessoryEquip.DisplayName;
			}
			
			statsScreen.baseAttack.text = String(baseStats.BaseAttack);
			statsScreen.baseDefense.text = String(baseStats.BaseDefense);
			statsScreen.baseMagicPower.text = String(baseStats.BaseMagicPwr);
			statsScreen.baseMagicDefense.text = String(baseStats.BaseMagicDef);
			statsScreen.baseMaxHP.text = String(baseStats.BaseMaxHealth);
			statsScreen.baseMaxMP.text = String(baseStats.BaseMaxMagic);
			statsScreen.baseAccuracy.text = String(baseStats.BaseAccuracy);
			statsScreen.baseEvasion.text = String(baseStats.BaseEvasion);
			statsScreen.baseSpeed.text = String(baseStats.BaseSpeed);
			statsScreen.baseLuck.text = String(baseStats.BaseLuck);
			
			statsScreen.equipAttack.text = String(baseStats.AttackBonus);
			statsScreen.equipDefense.text = String(baseStats.DefenseBonus);
			statsScreen.equipMagicPower.text = String(baseStats.MagicPwrBonus);
			statsScreen.equipMagicDefense.text = String(baseStats.MagicDefBonus);
			statsScreen.equipMaxHP.text = String(baseStats.MaxHPBonus);
			statsScreen.equipMaxMP.text = String(baseStats.MaxMPBonus);
			statsScreen.equipAccuracy.text = String(baseStats.AccuracyBonus);
			statsScreen.equipEvasion.text = String(baseStats.EvasionBonus);
			statsScreen.equipSpeed.text = String(baseStats.SpeedBonus);
			statsScreen.equipLuck.text = String(baseStats.LuckBonus);
			
			statsScreen.totalAttack.text = String(baseStats.Attack);
			statsScreen.totalDefense.text = String(baseStats.Defense);
			statsScreen.totalMagicPower.text = String(baseStats.MagicPwr);
			statsScreen.totalMagicDefense.text = String(baseStats.MagicDef);
			statsScreen.totalMaxHP.text = String(baseStats.MaxHealth);
			statsScreen.totalMaxMP.text = String(baseStats.MaxMagic);
			statsScreen.totalAccuracy.text = String(baseStats.Accuracy);
			statsScreen.totalEvasion.text = String(baseStats.Evasion);
			statsScreen.totalSpeed.text = String(baseStats.Speed);
			statsScreen.totalLuck.text = String(baseStats.Luck);

			var green = 0x006600;
			var red = 0xff0000;
			var black = 0x000000;
			
			if (comparision)
			{
				if (baseStats.AttackBonus > originalStats.AttackBonus)
					statsScreen.equipAttack.textColor = green;
				else if (baseStats.AttackBonus < originalStats.AttackBonus)
					statsScreen.equipAttack.textColor = red;
				if (baseStats.DefenseBonus > originalStats.DefenseBonus)
					statsScreen.equipDefense.textColor = green;
				else if (baseStats.DefenseBonus < originalStats.DefenseBonus)
					statsScreen.equipDefense.textColor = red;
				if (baseStats.MagicPwrBonus > originalStats.MagicPwrBonus)
					statsScreen.equipMagicPower.textColor = green;
				else if (baseStats.MagicPwrBonus < originalStats.MagicPwrBonus)
					statsScreen.equipMagicPower.textColor = red;
				if (baseStats.MagicDefBonus > originalStats.MagicDefBonus)
					statsScreen.equipMagicDefense.textColor = green;
				else if (baseStats.MagicDefBonus < originalStats.MagicDefBonus)
					statsScreen.equipMagicDefense.textColor = red;
				if (baseStats.MaxHPBonus > originalStats.MaxHPBonus)
					statsScreen.equipMaxHP.textColor = green;
				else if (baseStats.MaxHPBonus < originalStats.MaxHPBonus)
					statsScreen.equipMaxHP.textColor = red;
				if (baseStats.MaxMPBonus > originalStats.MaxMPBonus)
					statsScreen.equipMaxMP.textColor = green;
				else if (baseStats.MaxMPBonus < originalStats.MaxMPBonus)
					statsScreen.equipMaxMP.textColor = red;
				if (baseStats.AccuracyBonus > originalStats.AccuracyBonus)
					statsScreen.equipAccuracy.textColor = green;
				else if (baseStats.AccuracyBonus < originalStats.AccuracyBonus)
					statsScreen.equipAccuracy.textColor = red;
				if (baseStats.EvasionBonus > originalStats.EvasionBonus)
					statsScreen.equipEvasion.textColor = green;
				else if (baseStats.EvasionBonus < originalStats.EvasionBonus)
					statsScreen.equipEvasion.textColor = red;
				if (baseStats.SpeedBonus > originalStats.SpeedBonus)
					statsScreen.equipSpeed.textColor = green;
				else if (baseStats.SpeedBonus < originalStats.SpeedBonus)
					statsScreen.equipSpeed.textColor = red;
				if (baseStats.LuckBonus > originalStats.LuckBonus)
					statsScreen.equipLuck.textColor = green;
				else if (baseStats.LuckBonus < originalStats.LuckBonus)
					statsScreen.equipLuck.textColor = red;
				if (baseStats.Attack > originalStats.Attack)
					statsScreen.totalAttack.textColor = green;
				else if (baseStats.Attack < originalStats.Attack)
					statsScreen.totalAttack.textColor = red;
				if (baseStats.Defense > originalStats.Defense)
					statsScreen.totalDefense.textColor = green;
				else if (baseStats.Defense < originalStats.Defense)
					statsScreen.totalDefense.textColor = red;
				if (baseStats.MagicPwr > originalStats.MagicPwr)
					statsScreen.totalMagicPower.textColor = green;
				else if (baseStats.MagicPwr < originalStats.MagicPwr)
					statsScreen.totalMagicPower.textColor = red;
				if (baseStats.MagicDef > originalStats.MagicDef)
					statsScreen.totalMagicDefense.textColor = green;
				else if (baseStats.MagicDef < originalStats.MagicDef)
					statsScreen.totalMagicDefense.textColor = red;
				if (baseStats.MaxHealth > originalStats.MaxHealth)
					statsScreen.totalMaxHP.textColor = green;
				else if (baseStats.MaxHealth < originalStats.MaxHealth)
					statsScreen.totalMaxHP.textColor = red;
				if (baseStats.Accuracy > originalStats.Accuracy)
					statsScreen.totalAccuracy.textColor = green;
				else if (baseStats.Accuracy < originalStats.Accuracy)
					statsScreen.totalAccuracy.textColor = red;
				if (baseStats.Evasion > originalStats.Evasion)
					statsScreen.totalEvasion.textColor = green;
				else if (baseStats.Evasion < originalStats.Evasion)
					statsScreen.totalEvasion.textColor = red;
				if (baseStats.Speed > originalStats.Speed)
					statsScreen.totalSpeed.textColor = green;
				else if (baseStats.Speed < originalStats.Speed)
					statsScreen.totalSpeed.textColor = red;
				if (baseStats.Luck > originalStats.Luck)
					statsScreen.totalLuck.textColor = green;
				else if (baseStats.Luck < originalStats.Luck)
					statsScreen.totalLuck.textColor = red;
			}
			else
			{
				statsScreen.equipAttack.textColor = black;
				statsScreen.equipDefense.textColor = black;
				statsScreen.equipMagicPower.textColor = black;
				statsScreen.equipMagicDefense.textColor = black;
				statsScreen.equipMaxHP.textColor = black;
				statsScreen.equipMaxMP.textColor = black;
				statsScreen.equipAccuracy.textColor = black;
				statsScreen.equipEvasion.textColor = black;
				statsScreen.equipSpeed.textColor = black;
				statsScreen.equipLuck.textColor = black;
				statsScreen.totalAttack.textColor = black;
				statsScreen.totalDefense.textColor = black;
				statsScreen.totalMagicPower.textColor = black;
				statsScreen.totalMagicDefense.textColor = black;
				statsScreen.totalMaxHP.textColor = black;
				statsScreen.totalAccuracy.textColor = black;
				statsScreen.totalEvasion.textColor = black;
				statsScreen.totalSpeed.textColor = black;
				statsScreen.totalLuck.textColor = black;
			}
		}
		
		private function loadEquipment():void
		{
			removeMenuChildren();
			var tempEquipment:Array = new Array();
			
			var noEquip1:Equipment_None = new Equipment_None();
			noEquip1.DisplayName = rhandRemoveName;
			var noEquip2:Equipment_None = new Equipment_None();
			noEquip2.DisplayName = lhandRemoveName;
			var noEquip3:Equipment_None = new Equipment_None();
			noEquip3.DisplayName = bodyRemoveName;
			var noEquip4:Equipment_None = new Equipment_None();
			noEquip4.DisplayName = accessoryRemoveName;
			tempEquipment.push(noEquip1);
			tempEquipment.push(noEquip2);
			tempEquipment.push(noEquip3);
			tempEquipment.push(noEquip4);
			
			for(var j:int = 0; j < MainInfo.EquipmentNames.length; j++)
			{
				var copyEquip:Class = getDefinitionByName(MainInfo.EquipmentNames[j]) as Class;
				tempEquipment.push(new copyEquip);
			}
			
			var selectEquipMenu:MyObjectMenu = new MyObjectMenu(4, tempEquipment, 20, 20);
			equipMenuPlaceholder.addChild(selectEquipMenu);
		}
		
		private function removeMenuChildren():void
		{
			while (equipMenuPlaceholder.numChildren > 0)
			{
				equipMenuPlaceholder.removeChildAt(0);
			}
		}
	}
}